package dataextraction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class ScopusEngineLoader extends AbstractEngineLoader{

	private static final String SCOPUS_PUBLISHER = "SCOPUS";

	private static ScopusEngineLoader instance;

	private static int NUM_OF_ENTRIES = 1; 

	public static ScopusEngineLoader getInstance() {
		if (ScopusEngineLoader.instance == null) {
			ScopusEngineLoader.instance = new ScopusEngineLoader();
		}
		return ScopusEngineLoader.instance;
	}

	private ScopusEngineLoader() {

	}


	private Entry createEntry(String title, String url, String publicationAbstract, String[] pages){

		Entry entry = new Entry();

		entry.setDocumentTitle(title.substring(1,title.length()-1));//remove {} inicio/fim
		
		if(pages != null){
			try{
				String endPage = pages[1].substring(0,pages[1].length()-1);
				String startPage = pages[0].substring(1);

				entry.setNumberOfPages(Integer.parseInt(endPage)-Integer.parseInt(startPage)+1);
			}catch(NumberFormatException e){
				entry.setNumberOfPages(-1);//paginas final/inicial nao definida
			}catch (ArrayIndexOutOfBoundsException e) {
				entry.setNumberOfPages(-1);
			}
		}else{
			entry.setNumberOfPages(-1);
		}
		
		entry.setPublicationAbstract(publicationAbstract.substring(1,publicationAbstract.length()-2));//remove {} inicio/fim
		entry.setPdfLink(url);
		entry.setPublisher(SCOPUS_PUBLISHER);
		entry.setId("A_"+SCOPUS_PUBLISHER+"_"+NUM_OF_ENTRIES++);

		return entry;
	}


	public List<Entry> reader(File csvFile) throws IOException {

		FileInputStream stream = new FileInputStream(csvFile);  
		InputStreamReader streamReader = new InputStreamReader(stream);  
		BufferedReader reader  = new BufferedReader(streamReader);

		List<Entry> entriesOnFile = new ArrayList<Entry>();

		String line = null;
		String title = null;
		String url = null;
		String publicationAbstract = null;

		String[] pages = null;

		while ((line = reader.readLine()) != null) {  
			try{
				if(line.substring(0,6).equals("title=")){

					if(title != null){
						System.err.println("Adicionar entrada de forma manual: "+title);
						NUM_OF_ADD_MANUAL++;
					}

					title = line.substring(6, line.length()-1);
				}else if(line.substring(0,4).equals("url=")){
					url = line.substring(4, line.length()-1);
				}else if(line.substring(0,9).equals("abstract=")){
					publicationAbstract = line.substring(9);
				}else if(line.substring(0,6).equals("pages=")){
					String content = line.substring(6, line.length()-1);
					pages = content.split("-");
				}
			}catch(StringIndexOutOfBoundsException e){

			}

			if(title != null && url != null && publicationAbstract != null){
				Entry entry = createEntry(title,url,publicationAbstract,pages);


				if(entry.getDocumentTitle() != null && entry.getPublicationAbstract() != null){
					entriesOnFile.add(entry);
				}
				title = null;
				url = null;
				publicationAbstract = null;
			}

		}  

		reader.close();
		streamReader.close();
		stream.close();


		System.out.println("Fim do arquivo "+ csvFile.getName() + " # de entradas: "+entriesOnFile.size());


		return entriesOnFile;
	}

	@Override
	protected String getPublisher() {
		return SCOPUS_PUBLISHER;
	}

}
